package event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BasicConfig extends Config{
	private static final String EVENT_CONFIG_FILE = "EventConfig.xml";
	private static final String EVENT_ENTRY = "EventEntry";
	private static final String SCALE_FACTOR = "ScaleFactor";
	private static final String NAME = "Name";
	private static final String NUMBER = "Number";
	private static final String ATTRIBUTE = "Attribute";
	private static final String ATTR_NAME = "AttrName";
	private static final String TYPE = "Type";
	private static final String PARAMETER = "Parameter";
	private static final String FORMAT = "Format";
	private static final String MAX_TIME_STEP = "MaxTimeStep";
	private Map<String, Basic> basicMap = new HashMap<String,Basic>(); 
	private List<Basic> basicList = new ArrayList<Basic>();
	private int format;
	public BasicConfig()
	{
		super(EVENT_CONFIG_FILE);
		loadConfig();
	}
	//load configurations for page attributes and links
	protected void loadConfig()
	{
		readFile();
		Node scaleFactor = config.getElementsByTagName(SCALE_FACTOR).item(0);
		Node eventFormat = config.getElementsByTagName(FORMAT).item(0);
		format = Integer.parseInt(eventFormat.getFirstChild().getNodeValue());
		int sf = Integer.parseInt(scaleFactor.getFirstChild().getNodeValue());
		NodeList eventList = config.getElementsByTagName(EVENT_ENTRY);
		for(int i = 0; i < eventList.getLength(); i++)
		{
			Element eventEntry = (Element)(eventList.item(i));
			Basic basic = new Basic();
			Node eventName = eventEntry.getElementsByTagName(NAME).item(0);
			String name = eventName.getFirstChild().getNodeValue();
			basic.name = name;
			Node maxTimeStep = eventEntry.getElementsByTagName(MAX_TIME_STEP).item(0);
			basic.maxTimeStep = Integer.parseInt(maxTimeStep.getFirstChild().getNodeValue());
			Node eventNumber = eventEntry.getElementsByTagName(NUMBER).item(0);
			long number = Long.parseLong(eventNumber.getFirstChild().getNodeValue());
			basic.number = number * sf;

			NodeList attrNodeList = eventEntry.getElementsByTagName(ATTRIBUTE);
			List<Attribute> attrList = new LinkedList<Attribute>();
			Map<String, Attribute> attrMap = new HashMap<String, Attribute>();
			for(int j = 0; j < attrNodeList.getLength(); j++)
			{
				Attribute attr = new Attribute();
				Element attrNode = (Element)(attrNodeList.item(j));
				Node attrName = attrNode.getElementsByTagName(ATTR_NAME).item(0);
				attr.name = attrName.getFirstChild().getNodeValue();
				Node attrType = attrNode.getElementsByTagName(TYPE).item(0);
				attr.type = attrType.getFirstChild().getNodeValue(); 
				List<String> params = new LinkedList<String>();
				NodeList paramList = attrNode.getElementsByTagName(PARAMETER);
				for(int k = 0; k < paramList.getLength(); k++)
				{
					Node paramNode = paramList.item(k);
					String param = paramNode.getFirstChild().getNodeValue();
					params.add(param);
				}
				attr.params = params;
				attrList.add(attr);
				attrMap.put(attr.name, attr);
			}
			basic.attrList = attrList;
			basic.attrMap = attrMap;
			basicMap.put(basic.name, basic);
			basicList.add(basic);
		}
	}
	public Basic getBasic(String name)
	{
		return basicMap.get(name);
	}
	public Map<String,Basic> getBasicMap()
	{
		return basicMap;
	}
	public List<Basic> getBasicList()
	{
		return basicList;
	}
	public int getFormat() {
		return format;
	}
}
